<#include "license.ftl">
<@license/>
package ${doc.all.package}.service;

import redora.util.JSONWriter;

/**
* @author Redora (www.redora.net)
*/
public class ModelViewService {

    public static void getModelNames(JSONWriter writer) {
        writer.print('[');
        char comma = ' ';
        for (String object : Upgrade.objects) {
            writer.print(comma); comma = ',';
            writer.print('{');
            writer.quoted("objectName", object);
            writer.print('}');
        }
        writer.print(']');
    }

    public static void getModelFields(String modelName, JSONWriter writer){
        writer.print('{');
        writer.key("field");
        writer.print('[');
<#list doc["/all/object"] as model>
        if (modelName.equalsIgnoreCase("${model.@name}")) {
    <#assign n = 0 />
        <#list model.attributes?children as att>
            <#if att?node_type == "element">
                <#if att?node_name == "long" && att.@parentClass[0]??>
                <#else>
                    <#assign n = n+1 />
                    <#if n gt 1>
            writer.print(',');
                    </#if>
            //add field_name
            writer.print('{');
            writer.key("field_name");
                    <#if att?node_name != "set" && att?node_name != "object">
            writer.quoteValue("${att.@name[0]!att.@class?uncap_first}");
                    <#elseif att?node_name == "set">
            writer.quoteValue("${att.@plural}");
                    <#elseif att?node_name == "object">
            writer.quoteValue("${att.@name[0]!att.@class?uncap_first}");
                    </#if>
            writer.print(',');

            //add field_type 	
            writer.quoted("field_type", "${att?node_name}");
            writer.print(',');

            //add not_null attribute	
            writer.kvp("not_null", "${att.@notnull!"false"}");
            writer.print(',');

            //add default attribute
                <#if att.@default[0]??>
            writer.kvp("default","${att.@default}");
            writer.print(',');
                </#if>

            //add regexp attribute
                <#if att.@regexp[0]??>
            writer.kvp("regexp", "${att.@regexp}");
            writer.print(',');
                </#if>

            //add maxlength attribute				
                <#if att.@maxlength[0]??>
            writer.kvp("maxlength", "${att.@maxlength}");
            writer.print(',');
                </#if>

            //add finder attribute
            writer.kvp("finder", "<#if att.finder[0]??>true<#else>false</#if>");
            writer.print(',');

            //add lazy attribute
            writer.kvp("lazy", "${att.@lazy!"false"}");
            writer.print(',');

            //add list attribute
            writer.kvp("list", "${att.@list!"false"}");
            writer.print('}');
            </#if>
        </#if>
        </#list>
            }
    </#list>
        writer.print("],");

        writer.key("businessRule");
        writer.print('[');
    <#list doc["/all/object"] as model>
        if(modelName.equalsIgnoreCase("${model.@name}")){
        <#if model.businessRules[0]??>
            <#list model.businessRules.businessRule as att>
            writer.print('{');
            writer.kvp("number", "${att.@number}");
            writer.print(',');
            writer.kvp("javadoc", "${att.@javadoc}");
            writer.print(',');
            writer.key("attributes");
                <#if att.@attributes[0]??>
            writer.quoteValue("${att.@attributes}");
                <#else>
            writer.nullOut();
                </#if>
            writer.print('}');
                <#if att_has_next>
            writer.print(',');
                </#if>
            </#list>
        </#if>
        }
    </#list>
        writer.print("]}");
    }

    public static void getFindFields(String modelName, JSONWriter writer){
    //TODO check this where it is used
        writer.print('[');
<#list doc["/all/object"] as model>
        if (modelName.equalsIgnoreCase("${model.@name}")) {
    <#assign n = 0 />
    <#list model.attributes?children as att>
        <#if att.finder[0]??>
            <#assign n = n+1 />
            <#if n gt 1>
            writer.print(',');
        </#if>
            writer.print('{');
            writer.kvp("finder_field_name", "${att.@fieldName}");
            writer.print(',');
            writer.kvp("finder_field_method", "findBy${att.@fieldName?cap_first}");
            writer.print('}');
        </#if>
    </#list>
        }
</#list>
        writer.print(']');
    }
}
